package com.ybwh.datasource.refreshable.druid.spring.boot;

import com.ybwh.datasource.refreshable.druid.DruidRefreshDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.beans.ConstructorProperties;
import java.sql.SQLException;

/**
 * <p>
 * springboot 的自动配置
 * </p>
 *
 * @author fanbeibei
 * @version V1.0
 * @since 2019/4/8 17:53
 */
@Configuration
@Conditional(SpringBootRefreshDruidCondition.class)
@EnableConfigurationProperties({SpringBootDruidRefreshDataSourceProperties.class})
public class SpringBootDruidRefreshDataSourceConfiguration {

    private SpringBootDruidRefreshDataSourceProperties properties;


    @Bean(value = "dataSource", initMethod = "init", destroyMethod = "destroy")
    public DruidRefreshDataSource dataSource() throws SQLException {
        return new DruidRefreshDataSource(properties);
    }

    @ConstructorProperties({"properties"})
    public SpringBootDruidRefreshDataSourceConfiguration(SpringBootDruidRefreshDataSourceProperties properties) {
        this.properties = properties;
    }

}
